import { defineStore } from 'pinia'

export const useDataStore = defineStore('data', {
  state: () => ({
    homeData: null,
    dashboardData: null,
    userData: null,
    loading: false
  }),
  actions: {
    // 加载首页数据
    async loadHomeData() {
      this.loading = true
      try {
        // 模拟API请求
        const response = await new Promise((resolve) => {
          setTimeout(() => {
            resolve({
              stats: {
                totalUsers: 1250,
                totalOrders: 3480,
                revenue: 45600
              },
              recentActivities: [
                { id: 1, action: '用户注册', time: '10分钟前' },
                { id: 2, action: '订单创建', time: '25分钟前' },
                { id: 3, action: '用户登录', time: '1小时前' }
              ]
            })
          }, 600)
        })
        
        this.homeData = response
        return response
      } catch (error) {
        console.error('加载首页数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 加载数据面板数据
    async loadDashboardData() {
      this.loading = true
      try {
        // 模拟API请求
        const response = await new Promise((resolve) => {
          setTimeout(() => {
            resolve({
              salesChart: {
                labels: ['1月', '2月', '3月', '4月', '5月', '6月'],
                data: [1200, 1900, 1500, 2400, 2800, 3200]
              },
              userGrowth: {
                labels: ['1月', '2月', '3月', '4月', '5月', '6月'],
                data: [120, 190, 150, 240, 280, 320]
              },
              topProducts: [
                { id: 1, name: '产品A', sales: 1250 },
                { id: 2, name: '产品B', sales: 980 },
                { id: 3, name: '产品C', sales: 750 }
              ]
            })
          }, 800)
        })
        
        this.dashboardData = response
        return response
      } catch (error) {
        console.error('加载数据面板数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 加载用户管理数据
    async loadUserData() {
      this.loading = true
      try {
        // 模拟API请求
        const response = await new Promise((resolve) => {
          setTimeout(() => {
            resolve({
              users: [
                { id: 1, name: '张三', email: 'zhangsan@example.com', role: 'admin', status: 'active' },
                { id: 2, name: '李四', email: 'lisi@example.com', role: 'user', status: 'active' },
                { id: 3, name: '王五', email: 'wangwu@example.com', role: 'user', status: 'inactive' },
                { id: 4, name: '赵六', email: 'zhaoliu@example.com', role: 'user', status: 'active' }
              ],
              userStats: {
                total: 1250,
                active: 980,
                inactive: 270,
                newThisMonth: 45
              }
            })
          }, 700)
        })
        
        this.userData = response
        return response
      } catch (error) {
        console.error('加载用户数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    }
  }
})
